/*
 * Copyright(C) 2010 Luvina Software Company
 *
 * TestDemoManaged.java, Oct 29, 2010 ntmhuong
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.hibernate.User;
import net.luvina.manageuser.hibernate.UserGrp;
import net.luvina.manageuser.logic.TestLogic;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * List User bean managed
 *
 * @version 1.0
 * @author ntmhuong
 *
 */
@Component("testDemoManaged")
@Scope("request")
public class TestDemoManaged extends UserBaseManaged implements Serializable {
    private static final long serialVersionUID = 1L;
    public static final int LIMIT_RECORDS_PER_PAGE = 5;
    private List<SelectItem> itemGroups;
    private List<User> lsUser;
    private int userGrpId;
    private String userName;

    private int offset;
    private int limit;
    private int totalRecords;
    private int endPage;
    private int currentPage;

    @Autowired
    private TestLogic testLogic;

    /**
     * Default constructor
     */
    public TestDemoManaged() {
        // do nothing
    }

    /**
     * Init page
     *
     * @param event
     *            The PhaseEvent object
     */
    public final void initPage(PhaseEvent event) {
        if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {

            // Create data for selectbox group
            List<UserGrp> listUserGrp = this.testLogic.getGroupList();
            SelectItem selectItemUserGrp;
            this.itemGroups = new ArrayList<SelectItem>();
            itemGroups.add(new SelectItem("", ""));
            for (UserGrp userGrp : listUserGrp) {
                selectItemUserGrp = new SelectItem(userGrp.getUserGrpId(),
                        userGrp.getUserGrpName());
                this.itemGroups.add(selectItemUserGrp);
            }

            this.doSearch();
        }
    }

    /**
     * Action init list.
     *
     * @return The search page
     */
    public final String doInit() {

        // Create data for selectbox group
        List<UserGrp> listUserGrp = this.testLogic.getGroupList();
        SelectItem selectItemUserGrp;
        this.itemGroups = new ArrayList<SelectItem>();
        itemGroups.add(new SelectItem("", ""));
        for (UserGrp userGrp : listUserGrp) {
            selectItemUserGrp = new SelectItem(userGrp.getUserGrpId(), userGrp
                    .getUserGrpName());
            this.itemGroups.add(selectItemUserGrp);
        }
        this.doSearch();

        return CommonConstant.ADM002;
    }

    /**
     * Do search
     *
     * @return The search page
     */
    public final String doSearch() {
        System.out.println("============== Begin doSearch ==============");

        // get userInternalId from param
        HttpServletRequest request = (HttpServletRequest) FacesContext
                .getCurrentInstance().getExternalContext().getRequest();
        String strCurrentPage = request.getParameter("currentPage");
        if (strCurrentPage != null && !"".equals(strCurrentPage)) {
            this.currentPage = Integer.valueOf(strCurrentPage);
        }
        if (this.currentPage <= 0) {
            this.currentPage = 1;
        }
        this.offset = this.currentPage - 1;

        // Do search logic
        if (this.limit <= 0) {
            this.limit = LIMIT_RECORDS_PER_PAGE;
        }
        this.totalRecords = this.testLogic.getTotalRecords(userGrpId, userName);
        this.offset = this.offset * this.limit;
        this.lsUser = this.testLogic.getListUser(offset, limit, userGrpId,
                userName);
        this.endPage = this.getEndPage(totalRecords, limit);

        System.out.println("============== End doSearch ==============");
        return CommonConstant.ADM002;
    }

    /**
     * Get end page from total records and limit
     *
     * @param totalRecords
     *            Total records
     * @param limit
     *            Limit to get records
     * @return Integer end page
     */
    private int getEndPage(int totalRecords, int limit) {
        int endPage;
        if ((totalRecords % limit) == 0) {
            endPage = totalRecords / limit;
        } else {
            Double dEndPage = Math.floor((totalRecords / limit));
            endPage = dEndPage.intValue() + 1;
        }
        return endPage;
    }

    /**
     * @return the itemGroups
     */
    public List<SelectItem> getItemGroups() {
        return itemGroups;
    }

    /**
     * @param itemGroups
     *            the itemGroups to set
     */
    public void setItemGroups(List<SelectItem> itemGroups) {
        this.itemGroups = itemGroups;
    }

    /**
     * @return the lsUser
     */
    public List<User> getLsUser() {
        return lsUser;
    }

    /**
     * @param lsUser
     *            the lsUser to set
     */
    public void setLsUser(List<User> lsUser) {
        this.lsUser = lsUser;
    }

    /**
     * @return the userGrpId
     */
    public int getUserGrpId() {
        return userGrpId;
    }

    /**
     * @param userGrpId
     *            the userGrpId to set
     */
    public void setUserGrpId(int userGrpId) {
        this.userGrpId = userGrpId;
    }

    /**
     * @return the userName
     */
    public String getUserName() {
        return userName;
    }

    /**
     * @param userName
     *            the userName to set
     */
    public void setUserName(String userName) {
        this.userName = userName;
    }

    /**
     * @return the totalRecords
     */
    public int getTotalRecords() {
        return totalRecords;
    }

    /**
     * @param totalRecords
     *            the totalRecords to set
     */
    public void setTotalRecords(int totalRecords) {
        this.totalRecords = totalRecords;
    }

    /**
     * @return the endPage
     */
    public int getEndPage() {
        return endPage;
    }

    /**
     * @param endPage
     *            the endPage to set
     */
    public void setEndPage(int endPage) {
        this.endPage = endPage;
    }

    /**
     * @return the currentPage
     */
    public int getCurrentPage() {
        return currentPage;
    }

    /**
     * @param currentPage
     *            the currentPage to set
     */
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
}
